from django.db import models

class Author(models.Model):
    
    #publications = models.ManyToManyField(Publication)#, through='Authors2Publications')
    code = models.CharField(max_length=45,null=True)
    display_name = models.CharField(max_length=45,null=True)
    surname = models.CharField(max_length=45)
    second_surname = models.CharField(max_length=45,null=True)
    name = models.CharField(max_length=45)
    middle_name = models.CharField(max_length=45,null=True)
    department = models.CharField(max_length=45,null=True)
    email = models.CharField(max_length=45,null=True)
    category = models.CharField(max_length=45,null=True)
    search_names = models.CharField(max_length=255,null=True)
    gscholar_id = models.CharField(max_length=100,null=True)
    wok_id = models.CharField(max_length=100,null=True)
    scopus_id = models.CharField(max_length=100,null=True)
    
    #H-INDEX
    gscholar_hindex = models.IntegerField(max_length=5,default=0,null=True)
    wok_hindex = models.IntegerField(max_length=5,default=0,null=True)
    scopus_hindex = models.IntegerField(max_length=5,default=0,null=True)
    
    #Citation Count
    gscholar_cit_count = models.IntegerField(default=0,null=True)
    wok_cit_count = models.IntegerField(default=0,null=True)
    scopus_cit_count = models.IntegerField(default=0,null=True)
    
    #Publication Count
    gscholar_pub_count = models.IntegerField(default=0,null=True)
    wok_pub_count = models.IntegerField(default=0,null=True)
    scopus_pub_count = models.IntegerField(default=0,null=True)
    
   
    def __init__(self, *args, **kwargs):
        models.Model.__init__(self, *args, **kwargs)
        self.calculateHIndexsPubAndCitCount()
    
    def __unicode__(self):
        return u"%s %s" % (self.name,self.surname)


    def calculateHIndexsPubAndCitCount(self):
            authorPublications = self.publication_set.all()
            
            citesGS = []
            citesScopus = []
            citesWOK = []
            
            for publication in authorPublications:
                cite = int(publication.times_cited)
                if publication.source == "Google Scholar":
                    citesGS.append(cite)
                elif publication.source == "ISI WOK":
                    citesWOK.append(cite)
                elif publication.source == "Scopus":
                    citesScopus.append(cite)
                    
                    
            citesGS.sort(reverse=True)
            citesScopus.sort(reverse=True)
            citesWOK.sort(reverse=True)
            
            self.gscholar_pub_count = len(citesGS)
            self.wok_pub_count = len(citesWOK)
            self.scopus_pub_count = len(citesScopus)

            self.scopus_hindex = self.wok_hindex = self.gscholar_hindex = 0
            self.scopus_cit_count = self.wok_cit_count = self.gscholar_cit_count = 0
                        
            for cite in citesGS:
                if cite > self.gscholar_hindex:
                    self.gscholar_hindex += 1
                self.gscholar_cit_count += cite
            
            for cite in citesScopus:
                if cite > self.scopus_hindex:
                    self.scopus_hindex += 1
                self.scopus_cit_count += cite

            
            for cite in citesWOK:
                if cite > self.wok_hindex:
                    self.wok_hindex += 1
                self.wok_cit_count += cite
            
            self.save()
    
    class Meta:
        ordering = ('surname', 'name',)
        db_table = 'authors'
    

class Publication(models.Model):
    
    authors = models.ManyToManyField(Author)#, through='Authors2Publications')
    
    title = models.CharField(max_length=255)
    all_authors = models.TextField(null=True)
    source = models.CharField(max_length=45,null=True)
    times_cited = models.IntegerField()
    doc_type = models.CharField(max_length=45,null=True)
    publisher = models.CharField(max_length=150,null=True)
    abstract = models.TextField(null=True)
    pub_date = models.CharField(max_length=10, null=True)
    pub_type = models.CharField(max_length=45, null=True)
    pub_url = models.CharField(max_length=255, null=True)
    
    # Digital Object Identifier (DOI)
    doc_uid = models.CharField(max_length=255, null=True)
    
    
    def __unicode__(self):
        return self.title
    
    class Meta:
        ordering = ('title',)
        db_table = 'publications'

